package sitar.ui;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.Lib;
import flash.text.TextField;
import flash.text.TextFieldType;
import oboe.controllers.Controller;
import flash.text.TextFieldAutoSize;

/**
 * ...
 * @author Felipe Manga
 */

class Selector extends Controller
{

	public function new( signal:String, opts:Array< Array<Dynamic> > ) 
	{
		super( new Sprite() );
		
		Lib.current.stage.addChild( this.asset );
		
		var optHeight = Math.round( (Lib.current.stage.stageHeight - 10 - 10*opts.length ) / opts.length );
		var y:Int = Math.round( Lib.current.stage.stageHeight/2 - opts.length / 2 * (optHeight + 10) ) + 10;
		for ( i in 0 ... opts.length )
		{
			var opt = opts[i];
			var btn = new Sprite();
			var lbl = new TextField();
			lbl.text = opt[0];
			lbl.autoSize = TextFieldAutoSize.LEFT;
			lbl.x = 10;
			lbl.y = optHeight / 2 - lbl.textHeight / 2;
			lbl.type = TextFieldType.DYNAMIC;
			lbl.selectable = false;
			
			btn.y = y;
			btn.x = 10;
			btn.graphics.beginFill(0xA0A0F0, 0.7);
			btn.graphics.drawRect(0, 0, Lib.current.stage.stageWidth - 20, optHeight);
			btn.graphics.endFill();
			var THIS = this;
			btn.addEventListener( MouseEvent.CLICK, 
				function( e:Event ) 
				{ 
					var args = opt.slice(1);
					THIS.signalUp( signal, args );
					THIS.selfDestruct();
				}
			);
			btn.addChild( lbl );
			
			cast(this.asset, Sprite).addChild( btn );
			
			y += optHeight + 10;
		}
	}
	
}